// 格式化时间戳
export function formatUnixTime(val, type, isNewdate) {
	// that.formatUnixTime(xxx)
	var toDay, newDate
	if (!val) {
		return
	}
	if (isNewdate) {
		newDate = val
	} else {
		let n = 13 - val.toString().length
		newDate = new Date(val * Math.pow(10, n))
	}
	let Y = newDate.getFullYear()
	let M = ((newDate.getMonth() + 1) >= 10 ? (newDate.getMonth() + 1) : '0' + (newDate.getMonth() + 1))
	let D = (newDate.getDate() >= 10 ? newDate.getDate() : '0' + newDate.getDate())
	let h = newDate.getHours() >= 10 ? newDate.getHours() : '0' + newDate.getHours()
	let m = newDate.getMinutes() >= 10 ? newDate.getMinutes() : '0' + newDate.getMinutes()
	let s = newDate.getSeconds() >= 10 ? newDate.getSeconds() : '0' + newDate.getSeconds()
	if (!type) {
		toDay = Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s // 2018-10-26 10:06:21
	} else if (type === 1) {
		toDay = Y + '-' + M + '-' + D + ' ' + h + ':' + m // 2018-10-26 10:06
	} else if (type === 2) {
		toDay = Y + '-' + M + '-' + D // 2018-10-26
	} else if (type === 3) {
		toDay = M + '-' + D // 10-26
	} else if (type === 4) {
		toDay = [M, D, h, m] // [10,26,00,00]
	} else if (type === 5) {
		toDay = Y + '-' + M // 2020-01
	}
	return toDay
}
// 把字符串转换成unixtime，返回精确度为秒，非毫秒，并兼容IOS
export function dateParse(str) {
	var rst = Date.parse(str.replace(/-/g, '/')) || Date.parse(str)
	rst = rst / 1000
	return rst
}
// 防抖
export function _debounce(func,  wait) {
	let timer;
	return function() {
		let context = this; // 这边的 this 指向谁?
		let args = arguments; // arguments中存着e

		if (timer) clearTimeout(timer);

		let callNow = !timer;

		timer = setTimeout(() => {
			timer = null;
		}, wait)

		if (callNow) func.apply(context, args);
	}
}
// 节流
export function _throttle(fn, interval) {
	var last;
	var timer;
	var interval = interval || 400;
	return function() {
		var th = this;
		var args = arguments;
		var now = +new Date();
		if (last && now - last < interval) {
			clearTimeout(timer);
			timer = setTimeout(function() {
				last = now;
				fn.apply(th, args);
			}, interval);
		} else {
			last = now;
			fn.apply(th, args);
		}
	}
}

// 数组去空
export function arrTrimHandle(arr) {
	var rst = arr.filter(function(val) {
		return val && val.trim()
	})
	return rst
}
// 一个数组是否包含另个数组 type有值对比是否全等于
export function beIncludedArr(smallArr, bigArr, type) {
	var intersection,
		objArr,
		rst
	intersection = smallArr.filter(function(val) {
		val = val && val.trim()
		return bigArr.indexOf(val) > -1
	})
	objArr = type ? bigArr : smallArr
	if (type) console.log('intersection', intersection, objArr);
	if (intersection.length === objArr.length) {
		rst = true
	} else {
		rst = false
	}
	return rst
}
// 一个数组是否包含另个数组
export function equalityArr(arr1, arr2) {
	var rst
	if (arr1.sort().toString() === arr2.sort().toString()) {
		rst = true
	} else {
		rst = false
	}
	return rst
}
// 连续点击运行
export function quickClickRun(fn, that) {
	that.maxClick = 6
	that.limitTime = 3 * 1000
	var now = Date.parse(new Date())
	if (!that.click) {
		that.calculationTime = now
		that.click = 1
	} else {
		that.click++
		if ((that.click > that.maxClick) && (now - that.calculationTime < that.limitTime)) {
			if (fn && typeof fn === 'function') fn()
			that.click = null
			that.calculationTime = null
		} else if (now - that.calculationTime > that.limitTime) {
			that.click = null
			that.calculationTime = null
		}
	}
}
//获取年龄
export function IdCardAge(userCard) {
	var myDate = new Date();
	var month = myDate.getMonth() + 1;
	var day = myDate.getDate();
	var age = myDate.getFullYear() - userCard.substring(6, 10) - 1;
	if (userCard.substring(10, 12) < month || userCard.substring(10, 12) == month && userCard.substring(12,
			14) <= day) {
		age++;
	}
	return age
}
export function trimStr(str) {
	str = str.replace(/\s*/g, "");
	return str
}
